package com.itheima.ydd.controller;



import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_menu")
@Slf4j
public class SysMenuController {
    @Autowired
    private ISysMenuService iSysMenuService;

    /**
     * 菜单修改
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody SysMenu sysMenu) {
        log.info("菜单修改");
        iSysMenuService.updateById(sysMenu);
        return ResultData.ok("修改成功");
    }

    /**
     * 菜单删除
     *
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResultData delete(Integer id) {
        return iSysMenuService.removeWithOutPid(id);
    }

    /**
     * 获取所有菜单
     *
     * @return
     */
    @GetMapping("allData")
    public ResultData listAll() {
        return iSysMenuService.listWithChi();
    }

    /**
     * 添加菜单
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody SysMenu sysMenu) {
        log.info("填加菜单");
        sysMenu.setIsShow(1);
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setUpdateDate(LocalDateTime.now());
        iSysMenuService.save(sysMenu);
        R r = new R();
        r.setErrno(0);
        r.setErrmsg("菜单添加成功");
        return r;
    }

    /**
     * 菜单详情
     *
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(Integer id) {
        SysMenu menu = iSysMenuService.getById(id);
        return ResultData.ok(menu);
    }


}
